
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const coupon_image = "";

const url = 'https://weapp.whwangdoudou.cn/weapp.php';

const getInfo = data => {
  return new Promise(function (resolve, reject) {
    wx.showToast({
      title: '拼命加载中',
      icon: 'loading',
      success() {
        //网络请求
        wx.request({
          url: 'https://wxb.whwangdoudou.cn/pdd.php',
          data: data,
          method: 'POST',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            // success网络请求成功
            if (res.statusCode != 200) {
              reject({ error: '服务器忙，请稍后重试', code: 500 });
              return;
            }
            resolve(res.data);
            wx.hideToast();
          },
          fail: function (res) {
            reject({ error: '网络错误', code: 0 });
          }
        })
      }
    })
  })
}


const post = params => {
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: params,
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // success网络请求成功
        if (res.statusCode != 200) {
          reject({
            error: '服务器忙，请稍后重试',
            code: 500
          });
          return;
        }
        resolve(res.data);
      },
      fail: function (res) {
        reject({
          error: '网络错误',
          code: 0
        });
      }
    })
  })
}

const showModal = (that, options) => {
  let modalData = {
    content: options.content ? options.content : null,
    showCancel: options.showCancel ? options.showCancel : false,
    cancel_text: options.cancel_text ? options.cancel_text : '取消',
    ok_text: options.ok_text ? options.ok_text : '确定',
    image: options.image ? options.image : null,
    title: options.title ? options.title : null,
  }

  let animation = wx.createAnimation({
    duration: 200,  //动画时长
    timingFunction: "linear", //线性
    delay: 0  //0则不延迟
  });
  //animation.opacity(0).rotateX(-100).step();
  that.setData({
    modalData: modalData,
    showModal: true
  });
  that.setData({
    modalAnimation: animation.export()
  })
  setTimeout(function () {
    //animation.opacity(1).rotateX(0).step();
    that.setData({
      modalAnimation: animation
    })
  }.bind(this), 200)

  that.modalclose = function () {
    that.setData({
      showModal: false
    })
  }
  that.modalok = function () {
    that.setData({
      showModal: false
    })

  }
  if (options.success) {
    that.modalok = options.success
  }
}


/*获取当前页带参数的url*/
function getCurrentPageUrlWithArgs() {
  var pages = getCurrentPages()    //获取加载的页面
  var currentPage = pages[pages.length - 1]    //获取当前页面的对象
  var url = currentPage.route    //当前页面url
  var options = currentPage.options    //如果要获取url中所带的参数可以查看options

  //拼接url的参数
  var urlWithArgs = url + '?'
  for (var key in options) {
    var value = options[key]
    urlWithArgs += key + '=' + value + '&'
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

  return urlWithArgs
}



module.exports = {
  formatTime: formatTime,
  getInfo: getInfo,
  coupon_image: coupon_image,
  post: post,
  showModal: showModal,
  getCurrentPageUrlWithArgs: getCurrentPageUrlWithArgs
}
